﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Model;
using  DataAccess;

namespace Bussiness
{
    public class ProductManager
    {
        public static AssureMessage RegisterProduct(Product product,ProductStore store)
        {

            if (ProductDataAccess.GetProductById(product).Count == 0)
            {
                RegisterProductStore(store);
                return RegisterProduct1(product);
            }
            else
            {
                ModifyProductStore(store);
                return ModifyProduct(product);
            }
        }

        public static void RegisterProductStore(ProductStore store)
        {
            store.Product = null;
            store.StoreEnterDate = DateTime.Now.Date;
            DataAccess.DataAccessProductStore.InsertProductStore(store);
        }

        public static void ModifyProductStore(ProductStore store)
        {
            store.Product = null;
            store.StoreEnterDate = DateTime.Now.Date;
            DataAccessProductStore.UpdateProductStore(store);
        }

        private static AssureMessage RegisterProduct1(Product product)
        {
            AssureMessage assureMessage = new AssureMessage();
            try
            {
                if (product.ProductId == null || product.ProductId == string.Empty)
                {
                    assureMessage.state = false;
                    assureMessage.message = "product code is required";
                    return assureMessage;
                }

                if (ProductDataAccess.GetProductById(product).Count > 0)
                {
                    assureMessage.state = false;
                    assureMessage.message = "product code already exists";
                    return assureMessage;
                }

                ProductDataAccess.RegisterProduct(product);
                assureMessage.state = true;
                assureMessage.message = "succsefull register";
                //return assureMessage;
            }
            catch(Exception e)
            {
                assureMessage.state = false;
                assureMessage.message = e.Message;
            }
            return assureMessage;
        }

        public static AssureMessage ModifyProduct(Product product)
        {
            AssureMessage assureMessage = new AssureMessage();
            try
            {
                if (product.ProductId == null || product.ProductId == string.Empty)
                {
                    assureMessage.state = false;
                    assureMessage.message = "product code is required";
                    return assureMessage;
                }

                ProductDataAccess.ModifyProduct(product);
                assureMessage.state = true;
                assureMessage.message = "succsefull register";
            }
            catch (Exception e)
            {
                assureMessage.state = false;
                assureMessage.message = e.Message;
            }
            return assureMessage;
        }

        public static List<Product> GetProductList()
        {
            return ProductDataAccess.GetAllProducts();
        }

        public static int GetProductAvailabilityInStore(string productId)
        {
            var q= ProductDataAccess.GetProductInStore(productId);
            if (q.Count > 0)
                return q.First().StoreQuantity;
                return 0;
        }

        public static int GetProductAvailabilityInStock(string productId)
        {
            var q = ProductDataAccess.GetProductInStock(productId);
            if (q.Count > 0)
                return q.First().StoreQuantity;
            return 0;
        }

        public static List<Order> GetOrdersFromProduct(List<DropProduct> productDrop, string branchRemoteId)
        {
            var orders = new List<Order>();
            while (productDrop.Count!=0)
            {
                var p=productDrop[0];
                var ps = productDrop.Where(x => x.BranchId == p.BranchId).ToList();
                Order o = CreateOrderFromDetailsProductDrop(ps,branchRemoteId);
                o.Delivered = false;
                orders.Add(o);
                foreach (var dropProduct in ps)
                {
                    productDrop.Remove(dropProduct);
                }
            }
            return orders;
        }

        private static Order CreateOrderFromDetailsProductDrop(List<DropProduct> prods, string branchId)
        {
            var o=new Order(){BranchRemoteId = branchId, BranchLocalId = prods.First().BranchId};
            var os = prods.Select(dropProduct => new OrderDetail {ProductId = dropProduct.ProductId, Quantity = dropProduct.ProductQuantity}).ToList();
            o.OrderDetails = os;
            return o;
        }

        public static void SaveReserve(Order o)
        {
            ProductDataAccess.SaverOrder(o);
            ProductDataAccess.DiscountProducts(o);
        }

        public static void SaveSale(Sale dataSale)
        {
            ProductDataAccess.SaveSale(dataSale);
        }

        public static void RegisterCheckOutProduct(string branchId)
        {
            ProductDataAccess.CheckOutOrder(branchId);
        }

        public static Product GetProductById(string productId)
        {
            return ProductDataAccess.GetProductById(new Product {ProductId = productId}).First();
        }

        public static ProductStore GetProductStoreById(string productId)
        {
            var q = ProductDataAccess.GetProductInStore(productId);
            if(q.Count!=0)
                return q.First();
            
            return null;
        }

        public static List<string> GetAllBranchWithOrders()
        {
            return DataAccessProductStore.GetBranchWithOrders();
        }
    }
}
